# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from flask_user import UserMixin
from tank import db, app
from tank.core.documents import Document


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)

    # User authentication information (required for Flask-User)
    email = db.Column(db.Unicode(255), nullable=False, server_default=u'', unique=True)
    confirmed_at = db.Column(db.DateTime())
    password = db.Column(db.String(255), nullable=False, server_default='')
    reset_password_token = db.Column(db.String(100), nullable=False, server_default='')

    # User information
    active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0')
    first_name = db.Column(db.Unicode(50), nullable=False, server_default=u'')
    last_name = db.Column(db.Unicode(50), nullable=False, server_default=u'')

    # Relationships
    roles = db.relationship('Role', secondary='users_roles', backref=db.backref('users', lazy='dynamic'))

    @property
    def profile(self):
        return UserProfile.find_by_uid(self.id)

    @property
    def nickname(self):
        nickname = self.profile.get('nickname')
        if not nickname:
            nickname = self.email.split("@")[0]
        return nickname

    @property
    def avatar(self):
        return self.profile.get('avatar', app.config['DEFAULT_AVATAR'])


# Define the Role data model
class Role(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(50), nullable=False, server_default=u'', unique=True)  # for @roles_accepted()
    label = db.Column(db.Unicode(255), server_default=u'')  # for display purposes


# Define the UserRoles association model
class UsersRoles(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
    role_id = db.Column(db.Integer(), db.ForeignKey('role.id', ondelete='CASCADE'))


class UserInvitation(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    token = db.Column(db.Unicode(255), nullable=False, server_default=u'')
    email = db.Column(db.Unicode(255), nullable=False, server_default=u'')
    invited_by_user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
    invited_user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))



class UserProfile(Document):
    _name = 'user.profile'
    _schema = {
        "type": "object",
        "properties": {
            "uid": {"type": "number"},
            "nickname": {"type": "string"},
            "avatar": {"type": "string"},
            "gender": {
                "type": "number",
                "enum": [-1, 0, 1],
                "default": -1
            },
            "location": {
                "type": "array",
                "items": {"type": "string"},
                "maxItems": 2
            },
            "signature": {"type": 'string'}
        }
    }

    @classmethod
    def find_by_uid(cls, uid):
        return cls.find_one_or_create(uid=uid)
